package ltd.dujiabao.capability.domain.rulemng.expression;

import java.util.Optional;

import ltd.dujiabao.capability.common.utils.JsonUtils;
import ltd.dujiabao.capability.common.utils.MapUtils;
import ltd.dujiabao.capability.domain.capmng.cap.Capability;

/**
 * @author : DuJiabao
 * @Project : ddd-capability
 * @Time : 2024/4/13 12:24
 * @Desc :
 */
public abstract class BaseExpression implements Expression {
    protected final String module;
    protected final String key;
    protected final ExpressionType expressionType;
    protected final String target;

    public BaseExpression(String module, String key, ExpressionType expressionType, String target) {
        this.module = module;
        this.key = key;
        this.expressionType = expressionType;
        this.target = target;
    }

    public String getModule() {
        return module;
    }

    public String getKey() {
        return key;
    }

    public ExpressionType getExpressionType() {
        return expressionType;
    }

    public String getTarget() {
        return target;
    }

    protected Optional<String> findValue(Capability capability) {
        return Optional.ofNullable(capability.getModule(module))
                .map(o -> JsonUtils.bean2Map(o))
                .map(map -> MapUtils.deepGet(map, key))
                .map(String::valueOf);
    }
}
